package com.gw.controller;


import java.util.HashMap;
import java.util.Map;
import com.gw.base.BaseController;
import com.gw.base.informationValidator;
import com.gw.common.model.Information;
import com.gw.interceptor.LoginInterceptorAdmin;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

@Before(LoginInterceptorAdmin.class)
public class Manager_info extends BaseController {

	public void index(){
		render("info-list-mysql.html");
	}
	public void add(){
		render("info-add.html");
	}
	
	@Before(informationValidator.class)
	public void save(){
		getModel(Information.class).save();	
		redirect("/information");
	}
	
	public void edit(){
		setAttr("information",Information.dao.findById(getParaToInt(0)));
		render("info-edit.html");
	}
		
	@Before(informationValidator.class)
	public void update(){
		getModel(Information.class).update();			
		redirect("/information");
	}
	public void delete(){
		boolean b=Information.dao.deleteById(getPara(0));
		renderJson(b);
	}
	public void checkDelete(){
		String ids=getPara("ids");
		Db.use("jdbcUrl_mysql").update("delete from"+Information.dao.getTableName()+"where id in("+ids+")");
		boolean b=true;
		renderJson(b);
	}
	
	//mysql数据源
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void table_mysql(){
		int pageNumber = getParaToInt("curPage");
		int pageSize = getParaToInt("pageSize");
		String sortName = getPara("sortName");
		String sortOrder = getPara("sortOrder");
		String key = getPara("key");
		StringBuilder sqlExceptSelect = new StringBuilder();
		String tableName = Information.dao.getTableName();
		sqlExceptSelect.append("from " +tableName+ "where 1=1");
		if (StrKit.notBlank(key)) {
			sqlExceptSelect.append("and INFORMATIONTITLE like '%"+key+"%'");
		}
		if (StrKit.notBlank(sortName)) {
			sqlExceptSelect.append("order by " + sortName + " " + sortOrder);
		}
		
		Page<Information> page2 =Information.dao.paginate(pageNumber, pageSize, "select INFORMATION_ID,INFORMATIONTITLE,INFORMATIONSUBTITLE,INFORMATIONCONTENT,INFORMATIONISSUETIME,INFORMATIONMODIFYTIME,INFORMATIONVERSION,inforModifyMen", sqlExceptSelect.toString());

	//	Page<Record> page =Db.use("sqlserver").paginate(pageNumber, pageSize, "select INFORMATION_ID,INFORMATIONTITLE,INFORMATIONSUBTITLE,INFORMATIONCONTENT,INFORMATIONISSUETIME,INFORMATIONMODIFYTIME,INFORMATIONVERSION,inforModifyMen", sqlExceptSelect.toString());
		Map m = new HashMap();
		m.put("success", true);
		m.put("totalRows", page2.getTotalRow());
		m.put("curPage", pageNumber);
		m.put("data", page2.getList());		
		renderJson(m);
	}	
	
	//SqlServer数据源
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void table(){		
		int pageNumber = getParaToInt("curPage");
		int pageSize = getParaToInt("pageSize");
		String sortName = getPara("sortName");
		String sortOrder = getPara("sortOrder");
		String key = getPara("key");
		StringBuilder sqlExceptSelect = new StringBuilder();
		sqlExceptSelect.append("from ezoffice.OA_INFORMATION where 1=1 ");
		if (StrKit.notBlank(key)) {
			sqlExceptSelect.append("and INFORMATIONTITLE like '%"+key+"%'");
		}
		if (StrKit.notBlank(sortName)) {
			sqlExceptSelect.append("order by " + sortName + " " + sortOrder);
		}
		
//		Page<Information> page2 = Information.dao.paginate(pageNumber, pageSize, "select INFORMATION_ID,INFORMATIONTITLE,INFORMATIONSUBTITLE,INFORMATIONISSUETIME,INFORMATIONMODIFYTIME,INFORMATIONVERSION,inforModifyMen", sqlExceptSelect.toString());
		Page<Record> page = Db.use("sqlserver").paginate(pageNumber, pageSize, "select INFORMATION_ID,INFORMATIONTITLE,INFORMATIONSUBTITLE,INFORMATIONISSUETIME,INFORMATIONMODIFYTIME,INFORMATIONVERSION,inforModifyMen", sqlExceptSelect.toString());
		
		Map m = new HashMap();
		m.put("success", true);
		m.put("totalRows", page.getTotalRow());
		m.put("curPage", pageNumber);
		m.put("data", page.getList());
		
		renderJson(m);
	}
}




